package com.example.springboot.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.HoneyLogs;
import com.example.springboot.common.LogType;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Notice;
import com.example.springboot.entity.RepairForm;
import com.example.springboot.entity.User;
import com.example.springboot.entity.Worker;
import com.example.springboot.service.NoticeService;
import com.example.springboot.service.RepairService;
import com.example.springboot.service.UserService;
import com.example.springboot.service.WorkerService;
import com.example.springboot.utils.TokenUtils;
import com.sun.corba.se.spi.orbutil.threadpool.Work;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能：维修工的增删改查
 * 作者：zhoujun
 * 日期： 2024/5/4 23:07
 */

@RestController
@RequestMapping("/worker")
public class WorkerController {


    @Autowired
    WorkerService workerService;

    @HoneyLogs(operation = "维修工", type = LogType.ADD)
    @PostMapping("/add")
    public Result add(@RequestBody Worker worker){
        User currentUser = TokenUtils.getCurrentUser();  // 获取到当前登录的用户信息

//        repairForm.setTime(DateUtil.now());
//        repairForm.setName(currentUser.getUsername());
        workerService.save(worker);
        return Result.success();
    }

    @HoneyLogs(operation = "维修工", type = LogType.UPDATE)
    @PutMapping("/update")
    public Result update(@RequestBody Worker worker){
        workerService.updateById(worker);
        return Result.success();
    }

    @HoneyLogs(operation = "维修工", type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){

        workerService.removeById(id);
        return Result.success();
    }

    @HoneyLogs(operation = "维修工", type = LogType.BATCH_DELETE   )
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){

        workerService.removeBatchByIds(ids);
        return Result.success();
    }

    @GetMapping("/selectAll")
    public Result SelectAll(){
        List<Worker> WorkerList= workerService.list(new QueryWrapper<Worker>().orderByDesc("id"));
        return Result.success(WorkerList);
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Worker worker = workerService.getById(id);
        return Result.success(worker);
    }


    /**
     * 多条件模糊查询用户信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String workname) {
        QueryWrapper<Worker> queryWrapper = new QueryWrapper<Worker>();
        queryWrapper.like(StrUtil.isNotBlank(workname),"workname",workname);

        Page<Worker> page = workerService.page(new Page<>(pageNum, pageSize),queryWrapper);

        return Result.success(page);
    }
}